package com.xcl.controller;

import com.xcl.service.ShuntServiceInter;
import com.xcl.service.XclTailAfterServiceInter;
import com.zzg.entity.Tailafter;
import com.zzg.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ShuntController
 * @Description 分流入库控制器
 * @Author: xiaochaolei
 * @date 2021/7/2 10:39
 * @Version 1. 0
 */
@Controller
public class ShuntController {
    @Autowired
    ShuntServiceInter shuntServiceInter;

    @Autowired
    XclTailAfterServiceInter xclTailAfter;

    /**
     *得到未装载的数据
     * @param page 页码数
     * @param limit 条目数
     * @param waybill_id 运单号
     * @param substationUnit 到达分站
     * @param modeTransport 运输方式
     * @param startDate 起始日期
     * @param endingDate 到达日期
     * @return map类型未装载的集合
     */
    @GetMapping(value = "/getLoadingBYStateOneShuntController", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> getLoadingBYStateOne(int page, int limit, String waybill_id, String substationUnit, String modeTransport, String startDate, String endingDate, String id){
        //定义一个map集合
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        if (id==null){
            map.put("count",shuntServiceInter.getLoadingBYStateOne().size());
            map.put("data",shuntServiceInter.getLoadingBYStateOneLimit(page,limit));
            return map;
        }else {
            map.put("count",shuntServiceInter.getLoadingByWhere(waybill_id,substationUnit,modeTransport,startDate,endingDate,page,limit).size());
            map.put("data",shuntServiceInter.getLoadingByWhere(waybill_id,substationUnit,modeTransport,startDate,endingDate,page,limit));
            return map;
        }

    }


    /**
     *得到运输中的数据
     * @param page 页码数
     * @param limit 条目数
     * @param waybill_id 运单号
     * @param modeTransport 运输方式
     * @param id 判断是否为高级查询
     * @return map类型运输中的集合
     */
    @GetMapping (value = "/getLoadingBYStateTwoShuntController", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> getLoadingBYStateTwo(int page,int limit,String waybill_id,String  modeTransport,String id){
        //定义一个map集合
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        if (id==null){
            map.put("count",shuntServiceInter.getLoadingBYStateTwo().size());
            map.put("data",shuntServiceInter.getLoadingBYStateTwoLimit(page,limit));
            return map;
        }else {
            map.put("count", shuntServiceInter.getLoadingByWhereTwo(waybill_id,modeTransport,page,limit).size());
            map.put("data", shuntServiceInter.getLoadingByWhereTwo(waybill_id,modeTransport,page,limit));
            return map;
        }
    }

    /**
     *得到装载中的数据
     * @param page 页码数
     * @param limit 条目数
     * @return map类型装载中的集合
     */
    @GetMapping (value = "/getLoadingBYStateThreeShuntController", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> getLoadingBYStateThree(int page,int limit){
        //定义一个map集合
        Map<String,Object> map = new HashMap<>();
        map.put("code","0");
        map.put("msg","0");
        map.put("count",shuntServiceInter.getLoadingBYStateThree().size());
        map.put("data",shuntServiceInter.getLoadingBYStateThreeLimit(page,limit));
        return map;
    }

    /**
     * 多条修改
     * @param strings 需要修改的数据集合
     */
    @PostMapping(value = "/ModificationOneShuntController", produces = "application/text;charset=utf-8")
    @ResponseBody
    public void ModificationOne(String strings){
        //根据字符串分割
        String[] split= strings.split("-");
        //循环选择跟踪信息
        for (int i = 0; i < split.length; i++) {
            Tailafter tailafter=new Tailafter();
            //设置跟踪信息的id
            tailafter.setTailafter(UuidUtil.getUuid());
            //获得当前时间
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String hehe = dateFormat.format( now );
            //设置跟踪时间
            tailafter.setTailafterTime(hehe);
            //设置运单号
            tailafter.setWaybillId(split[i]);
            //设置状态
            tailafter.setState("待卸货");
            //选择跟踪信息
            xclTailAfter.insertXclTailAfter(tailafter);
        }
        //修改
        shuntServiceInter.upteModificationOne(split);
    }


    /**
     * 多条修改
     * @param strings 需要修改的数据集合
     */
    @PostMapping(value = "/upteModificationTwoShuntController", produces = "application/text;charset=utf-8")
    @ResponseBody
    public void upteModificationTwo(String strings){
        //根据字符串分割
        String[] split= strings.split("-");
        //循环选择跟踪信息
        for (int i = 0; i < split.length; i++) {
            Tailafter tailafter=new Tailafter();
            //设置跟踪信息的id
            tailafter.setTailafter(UuidUtil.getUuid());
            //获得当前时间
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String hehe = dateFormat.format( now );
            //设置跟踪时间
            tailafter.setTailafterTime(hehe);
            //设置运单号
            tailafter.setWaybillId(split[i]);
            //设置状态
            tailafter.setState("卸货完毕");
            //选择跟踪信息
            xclTailAfter.insertXclTailAfter(tailafter);
        }
        //修改
        shuntServiceInter.upteModificationTwo(split);
    }




    /**
     * 多条修改
     * @param strings 需要修改的数据集合
     */
    @PostMapping(value = "/upteModificationThreeShuntController", produces = "application/text;charset=utf-8")
    @ResponseBody
    public void upteModificationThree(String strings){
        //根据字符串分割
        String[] split= strings.split("-");
        //循环选择跟踪信息
        for (int i = 0; i < split.length; i++) {
            Tailafter tailafter=new Tailafter();
            //设置跟踪信息的id
            tailafter.setTailafter(UuidUtil.getUuid());
            //获得当前时间
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String hehe = dateFormat.format( now );
            //设置跟踪时间
            tailafter.setTailafterTime(hehe);
            //设置运单号
            tailafter.setWaybillId(split[i]);
            //设置状态
            tailafter.setState("直接签收");
            //选择跟踪信息
            xclTailAfter.insertXclTailAfter(tailafter);
        }
        //修改
        shuntServiceInter.upteModificationThree(split);
    }





}
